﻿using System;
using System.Drawing.Printing;
using System.Web.Http;
 // 你的 .NET 3.5 DLL 的命名空间

namespace LegacyBridgeService.Controllers
{
    [RoutePrefix("api/legacy")]
    public class LegacyController : ApiController
    {
        [HttpPost]
        [Route("process")]
        public IHttpActionResult ProcessData([FromBody] ProcessRequest request)
        {
            try
            {

                string REC_CREATE_TIME = "";
                string REC_REVISE_TIME = "";

                string ORDER_NO = "";
                string STACKING_NO = "";
                string MAT_NO = "";


                //获取前台传入的表集合
                //DataTable table_rec = JsonConvert.DeserializeObject<DataTable>(MAT_NO, new DingApi.ReadAheadDataTableConverter()); ;


                //分页web；
                string page_no ="1";
                string page_size = "100";


                //与windorm 传统兼容；
                string recordFrom = "0";
                string pageSize = "100";




                EI.EIInfo inBlock = new EI.EIInfo();
                inBlock.Tables[0].Columns.Add("REC_CREATE_TIME");
                inBlock.Tables[0].Columns.Add("REC_REVISE_TIME");

                inBlock.Tables[0].Columns.Add("ORDER_NO");
                inBlock.Tables[0].Columns.Add("STACKING_NO");
                inBlock.Tables[0].Columns.Add("POUND_NO");

                inBlock.Tables[0].Columns.Add("MAT_NO");


                inBlock.Tables[0].Rows.Add();


                inBlock.Tables[0].Rows[0]["REC_CREATE_TIME"] = REC_CREATE_TIME;//DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd 00:00:00");
                inBlock.Tables[0].Rows[0]["REC_REVISE_TIME"] = REC_REVISE_TIME;//DateTime.Now.ToString("yyyy-MM-dd 23:59:59");

                inBlock.Tables[0].Rows[0]["ORDER_NO"] = ORDER_NO;
                inBlock.Tables[0].Rows[0]["STACKING_NO"] = STACKING_NO;
                inBlock.Tables[0].Rows[0]["POUND_NO"] = STACKING_NO;
                inBlock.Tables[0].Rows[0]["MAT_NO"] = MAT_NO;




                inBlock.sys_info.company_code = "XN";
                inBlock.sys_info.company_name = "asf";//必须为WEB
                inBlock.sys_info.userid = "web";//
                inBlock.sys_info.username = "web";//[通过其他参数获取到；]   
                inBlock.Tables[0].TableName = "INQ";



                EF.DSUtility.PageInfoDataTable PageInfo = new EF.DSUtility.PageInfoDataTable();

                //winform分页兼容调用
                if (recordFrom != string.Empty && pageSize != string.Empty)
                {
                    PageInfo.AddPageInfoRow(int.Parse(recordFrom), int.Parse(pageSize));
                }
                else
                {
                    PageInfo.AddPageInfoRow((int.Parse(page_no) - 1) * int.Parse(page_size), int.Parse(page_size));
                }


                inBlock.Tables.Add("PageInfo"); //后台需要
                inBlock.Tables["PageInfo"].Merge(PageInfo);


                EI.EIInfo outBlock = EI.EITuxedo.CallService("qmcc801_save", inBlock);


                return Ok(new ProcessResponse
                {
                    Success = true,
                    Result = "asdfp",
                    Message = "处理成功"
                });
            }
            catch (System.Exception ex)
            {
                return Ok(new ProcessResponse
                {
                    Success = false,
                    Result = null,
                    Message = $"错误: {ex.Message}"
                });
            }
        }

        [HttpPost]
        [Route("calculate")]
        public IHttpActionResult Calculate([FromBody] CalculateRequest request)
        {
            try
            {

                string REC_CREATE_TIME = "";
                string REC_REVISE_TIME = "";

                string ORDER_NO = "";
                string STACKING_NO = "";
                string MAT_NO = "";


                //获取前台传入的表集合
                //DataTable table_rec = JsonConvert.DeserializeObject<DataTable>(MAT_NO, new DingApi.ReadAheadDataTableConverter()); ;


                //分页web；
                string page_no = "1";
                string page_size = "100";


                //与windorm 传统兼容；
                string recordFrom = "0";
                string pageSize = "100";




                EI.EIInfo inBlock = new EI.EIInfo();
                inBlock.Tables[0].Columns.Add("REC_CREATE_TIME");
                inBlock.Tables[0].Columns.Add("REC_REVISE_TIME");

                inBlock.Tables[0].Columns.Add("ORDER_NO");
                inBlock.Tables[0].Columns.Add("STACKING_NO");
                inBlock.Tables[0].Columns.Add("POUND_NO");

                inBlock.Tables[0].Columns.Add("MAT_NO");


                inBlock.Tables[0].Rows.Add();


                inBlock.Tables[0].Rows[0]["REC_CREATE_TIME"] = REC_CREATE_TIME;//DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd 00:00:00");
                inBlock.Tables[0].Rows[0]["REC_REVISE_TIME"] = REC_REVISE_TIME;//DateTime.Now.ToString("yyyy-MM-dd 23:59:59");

                inBlock.Tables[0].Rows[0]["ORDER_NO"] = ORDER_NO;
                inBlock.Tables[0].Rows[0]["STACKING_NO"] = STACKING_NO;
                inBlock.Tables[0].Rows[0]["POUND_NO"] = STACKING_NO;
                inBlock.Tables[0].Rows[0]["MAT_NO"] = MAT_NO;




                inBlock.sys_info.company_code = "XN";
                inBlock.sys_info.company_name = "asf";//必须为WEB
                inBlock.sys_info.userid = "web";//
                inBlock.sys_info.username = "web";//[通过其他参数获取到；]   
                inBlock.Tables[0].TableName = "INQ";



                EF.DSUtility.PageInfoDataTable PageInfo = new EF.DSUtility.PageInfoDataTable();

                //winform分页兼容调用
                if (recordFrom != string.Empty && pageSize != string.Empty)
                {
                    PageInfo.AddPageInfoRow(int.Parse(recordFrom), int.Parse(pageSize));
                }
                else
                {
                    PageInfo.AddPageInfoRow((int.Parse(page_no) - 1) * int.Parse(page_size), int.Parse(page_size));
                }


                inBlock.Tables.Add("PageInfo"); //后台需要
                inBlock.Tables["PageInfo"].Merge(PageInfo);


                EI.EIInfo outBlock = EI.EITuxedo.CallService("qmcc801_save", inBlock);


                return Ok(new ProcessResponse
                {
                    Success = true,
                    Result = "asdfp",
                    Message = "处理成功"
                });
            }
            catch (System.Exception ex)
            {
                return Ok(new ProcessResponse
                {
                    Success = false,
                    Result = null,
                    Message = $"错误: {ex.Message}"
                });
            }
        }

        [HttpGet]
        [Route("health")]
        public IHttpActionResult HealthCheck()
        {
            return Ok(new { Status = "运行正常", Timestamp = DateTime.Now });
        }
    }

    // 请求响应模型
    public class ProcessRequest
    {
        public string InputData { get; set; }
    }

    public class ProcessResponse
    {
        public bool Success { get; set; }
        public string Result { get; set; }
        public string Message { get; set; }
    }

    public class CalculateRequest
    {
        public int Number1 { get; set; }
        public int Number2 { get; set; }
    }

    public class CalculateResponse
    {
        public bool Success { get; set; }
        public int Result { get; set; }
        public string Error { get; set; }
    }
}